﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;
using static OpenIddict.Abstractions.OpenIddictConstants;
using WMS.Seven.Qi.Dto;
using Volo.Abp.Application.Dtos;

namespace WMS.Seven.Qi
{
    public class LossReportingService : ApplicationService, ILossReportingService
    {
        private readonly IRepository<LossReportingManagement, int> _lossReportingRepository;
        public LossReportingService(IRepository<LossReportingManagement, int> lossReportingRepository)
        {
            _lossReportingRepository = lossReportingRepository;
        }

        /// <summary>
        /// 获取损耗报告数据
        /// </summary>
        /// <param name="lossReportingDTO"></param>
        /// <returns></returns>
        public async Task<object> GetLossReportingData(RequestLossReportingDTO lossReportingDTO)
        {
            var lossReportingList = await _lossReportingRepository.GetQueryableAsync();
            lossReportingList = lossReportingList.WhereIf(!lossReportingDTO.ReportNumber.IsNullOrEmpty(), x => x.ReportNumber.Contains(lossReportingDTO.ReportNumber));
            lossReportingList = lossReportingList.WhereIf(!lossReportingDTO.DamageType.IsNullOrEmpty(), x => x.DamageType.Contains(lossReportingDTO.DamageType));


            //判断状态
            switch (lossReportingDTO.Status)
            {
                case 0:
                    lossReportingList = lossReportingList;
                    break;
                case 1:
                    lossReportingList = lossReportingList.Where(x => x.Status == 1);
                    break;
                case 2:
                    lossReportingList = lossReportingList.Where(x => x.Status == 2);
                    break;
                case 3:
                    lossReportingList = lossReportingList.Where(x => x.Status == 3);
                    break;
                default:
                    break;
            }
            var count = lossReportingList.Count();
            lossReportingList = lossReportingList.OrderBy(x => x.Id).Skip(((lossReportingDTO.Page - 1) ?? 0) * (lossReportingDTO.Limit ?? 10)).Take(lossReportingDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = lossReportingList.ToList(),
                msg = "",
            };
            return result;
        }

        /// <summary>
        /// 创建损耗报告
        /// </summary>
        /// <param name="createLossReportingDTO"></param>
        /// <returns></returns>
        public async Task<bool> CreateLossReporting(CreateLossReportingDTO createLossReportingDTO)
        {
            createLossReportingDTO.Status = 1;
            createLossReportingDTO.ProductName = "肉羊卷";
            createLossReportingDTO.ProductNumber = "33333";
            createLossReportingDTO.BatchNumber = "44444";
            createLossReportingDTO.Unit = "盒";
            createLossReportingDTO.Quantity = 2;

            createLossReportingDTO.ReportNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            var lossReportingEntity = AutoMapperExt.MapTo<LossReportingManagement>(createLossReportingDTO);
            await _lossReportingRepository.InsertAsync(lossReportingEntity);
            return true;
        }
        /// <summary>
        /// 更新损耗报告
        /// </summary>
        /// <param name="updateLossReportingDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdateLossReporting(UpdateLossReportingDTO updateLossReportingDTO)
        {
            var lossReporting = AutoMapperExt.MapTo<LossReportingManagement>(updateLossReportingDTO);
            await _lossReportingRepository.UpdateAsync(lossReporting);
            return true;
        }

        /// <summary>
        /// 删除损耗报告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteLossReporting(int id)
        {
            var lossReporting = await _lossReportingRepository.GetAsync(id);
            await _lossReportingRepository.DeleteAsync(lossReporting);
            return true;
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> UpdateStutes(int id)
        {
            var lossReporting = await _lossReportingRepository.GetAsync(id);
            if (lossReporting.Status == 1)
            {
                lossReporting.Status = 3;
            }
            else if (lossReporting.Status == 3)
            {
                lossReporting.Status = 2;
            }
            else if (lossReporting.Status == 2)
            {
                lossReporting.Status = 1;
            }
            await _lossReportingRepository.UpdateAsync(lossReporting);
            return 1;
        }


    }
}
